function [query_data, query_duration_ret] = query_loadprofile(duration_array, data_array, query_duration, method_id, extrapolation)
%query_loadprofile 按照方案进行时序运行曲线预处理
%   duration_array 字符串向量，表示曲线时间点，格式"hh:mm:ss"
%   data_array 浮点数向量，表示曲线值
%   query_duration 字符串向量，表示查询时间点，格式"hh:mm:ss"
%   method_id，插值方法，0-线性插值，1-保持上一个点
%   extrapolation，外插策略，0-保持最近一个时间点的系数，1-重复曲线

%转换为持续时间数组
if isstring(duration_array)
    duration_array = duration(duration_array, 'InputFormat', "hh:mm:ss");
end

if isstring(query_duration)
    query_duration = duration(query_duration, 'InputFormat', "hh:mm:ss");
end


%执行外插策略
[duration_array,Idx]=sort(duration_array,'ascend');
T=duration_array(end)-duration_array(1);
data_array=data_array(Idx);
query_duration=sort(query_duration);
query_duration_ret=query_duration;
query_data=zeros(size(query_duration));
switch extrapolation
    case 0
        idx=query_duration<duration_array(1);
        query_data(idx)=data_array(1);
        idx=query_duration>duration_array(end);
        query_data(idx)=data_array(end);
    case 1
        idx=query_duration<duration_array(1);
        while ~isempty(find(idx, 1))
            query_duration(idx)=query_duration(idx)+T;
            idx=query_duration<duration_array(1);
        end
        idx=query_duration>duration_array(end);
        while ~isempty(find(idx, 1))
            query_duration(idx)=query_duration(idx)-T;
            idx=query_duration>duration_array(end);
        end
    otherwise
        error('unknown extrapolation');
end


%插值
idx=query_duration>=duration_array(1) & query_duration<=duration_array(end);
switch method_id
    case 0
        query_data(idx) = interp1(duration_array,data_array,query_duration(idx),'linear');
    case 1
        query_data(idx) = interp1(duration_array,data_array,query_duration(idx),'previous');
    otherwise
        error('unknown method_id');
end

end

